Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Global and Local Variables

Python Functions

Global and Local Variables

Global and Local Variables in Python Functions

Python's scope rules determine where and how variables are accessible within your code. Understanding this is crucial for writing clean, predictable, and bug-free programs. The two primary scopes are global and local.

1. Global Variables

Global variables are declared outside any function. They are accessible from anywhere in your code, both inside and outside functions, after their definition. Example 1: Accessing a global variable inside a function
Accessing a global variable inside a function in python global_var = 10 # Global variable declaration def my_function(): print(f"Inside function: global_var = {global_var}") my_function() print(f"Outside function: global_var = {global_var}")

Output

Inside function: global_var = 10 Outside function: global_var = 10
Example 2: Modifying a global variable inside a function (requires `global` keyword) If you want to modify a global variable from within a function, you must explicitly declare it using the `global` keyword. Otherwise, Python will treat it as a new local variable.
Modifying a global variable inside a function in python global_var = 10 def modify_global(): global global_var # Declare global_var as global global_var = 20 modify_global() print(f"After modification: global_var = {global_var}")

Output

After modification: global_var = 20
Without the `global` keyword:
Modify a global variable in a function without global keyword in python global_var = 10 def modify_global_incorrectly(): global_var = 20 # This creates a NEW local variable modify_global_incorrectly() print(f"After incorrect modification: global_var = {global_var}")

Output

After incorrect modification: global_var = 10 (global variable remains unchanged)

2. Local Variables

Local variables are declared inside a function. They are only accessible within that specific function. They are created when the function is called and destroyed when the function finishes execution. Example 3: Local variable scope
Python functions local variable def my_other_function(): local_var = 5 # Local variable print(f"Inside function: local_var = {local_var}") my_other_function() # print(local_var) # This will cause an error: NameError: name 'local_var' is not defined

Output

Inside function: local_var = 5

Example 4: Nested functions and variable scope

Nested functions (functions defined inside other functions) inherit the scope of their enclosing functions.
Nested functions and variable scope in python def outer_function(): outer_var = 100 def inner_function(): inner_var = 50 print(f"Inner function: outer_var = {outer_var}, inner_var = {inner_var}") inner_function() # print(inner_var) # This will cause an error: NameError: name 'inner_var' is not defined outer_function()

Output

Inner function: outer_var = 100, inner_var = 50
The `inner_function` can access `outer_var`, but not vice-versa. `inner_var` is only accessible within `inner_function`.

Example 5: Illustrating the LEGB Rule

Python's scope resolution follows the LEGB rule:
Python's scope resolution with LEGB rule x = 100 # Global variable def outer(): x = 200 # Enclosing function local def inner(): x = 300 # Local variable print(f"Inner: x = {x}") inner() print(f"Outer: x = {x}") outer() print(f"Global: x = {x}")

Output

Inner: x = 300 Outer: x = 200 Global: x = 100
This example clearly demonstrates how the LEGB rule determines which `x` is accessed at each stage.

Tutorials